package com.qiyi.player.core.video.provider.actors.clientp2p
{
   import flash.events.EventDispatcher;
   import com.qiyi.player.core.IDestroy;
   import flash.utils.ByteArray;
   import com.qiyi.player.base.logging.ILogger;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.events.ProgressEvent;
   import com.qiyi.player.core.video.events.ClientP2PEvent;
   import com.qiyi.player.base.logging.Log;
   import flash.utils.Endian;
   
   public class ClientP2PMessager extends EventDispatcher implements IDestroy
   {
      
      public function ClientP2PMessager() {
         this._log = Log.getLogger("com.qiyi.player.core.video.provider.actors.clientp2p.ClientP2PMessager");
         super();
         this._socket = new ClientP2PSocket();
         this._socket.endian = Endian.BIG_ENDIAN;
         this._socket.addEventListener(Event.CONNECT,this.onConnectHandler);
         this._socket.addEventListener(IOErrorEvent.IO_ERROR,this.onIOErrorHandler);
         this._socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onSecurityErrorHandler);
         this._socket.addEventListener(Event.CLOSE,this.onCloseHandler);
         this._socket.addEventListener(ProgressEvent.SOCKET_DATA,this.onSocketDataHandler);
         this._socketData = new ByteArray();
         this._socketData.endian = Endian.BIG_ENDIAN;
         this._cacheData = new ByteArray();
         this._cacheData.endian = Endian.BIG_ENDIAN;
      }
      
      public static const FLAG_TYPE_COMMAND:int = 0;
      
      public static const FLAG_TYPE_VIDEO:int = 1;
      
      public static const HEAD_BYTE_SIZE:int = 5;
      
      public static const FLAG_BYTE_SIZE:int = 1;
      
      private var _socket:ClientP2PSocket;
      
      private var _socketData:ByteArray;
      
      private var _cacheData:ByteArray;
      
      private var _log:ILogger;
      
      public function get connected() : Boolean {
         return this._socket.connected;
      }
      
      public function connect(param1:String, param2:int) : void {
         if(!this._socket.connected)
         {
            this._socket.connect(param1,param2);
            this._log.debug("ClientP2PMessager start connect!");
         }
      }
      
      public function close() : void {
         this.checkClose();
      }
      
      public function send(param1:int, param2:ByteArray) : void {
         if((param2) && (this._socket.connected))
         {
            this._socket.writeByte(param1);
            this._socket.writeInt(param2.length);
            this._socket.writeBytes(param2);
            this._socket.flush();
         }
      }
      
      public function destroy() : void {
         this._log.info("ClientP2PMessager destroy!");
         this._socket.removeEventListener(Event.CONNECT,this.onConnectHandler);
         this._socket.removeEventListener(IOErrorEvent.IO_ERROR,this.onIOErrorHandler);
         this._socket.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onSecurityErrorHandler);
         this._socket.removeEventListener(Event.CLOSE,this.onCloseHandler);
         this._socket.removeEventListener(ProgressEvent.SOCKET_DATA,this.onSocketDataHandler);
         if(this._socket.connected)
         {
            this._socket.close();
         }
      }
      
      private function checkClose() : void {
         if(this._socket.connected)
         {
            this._socket.close();
            this._log.debug("ClientP2PMessager closed!");
         }
      }
      
      private function onConnectHandler(param1:Event) : void {
         this._log.info("ClientP2PMessager connect successful!");
         dispatchEvent(new ClientP2PEvent(ClientP2PEvent.Evt_Connected));
      }
      
      private function onIOErrorHandler(param1:IOErrorEvent) : void {
         this._log.debug("ClientP2PMessager IO error!");
         this.checkClose();
         dispatchEvent(new ClientP2PEvent(ClientP2PEvent.Evt_IOError));
      }
      
      private function onSecurityErrorHandler(param1:Event) : void {
         this._log.debug("ClientP2PMessager security error!");
         this.checkClose();
         dispatchEvent(new ClientP2PEvent(ClientP2PEvent.Evt_SecurityError));
      }
      
      private function onCloseHandler(param1:Event) : void {
         this._log.info("ClientP2PMessager server close!");
         dispatchEvent(new ClientP2PEvent(ClientP2PEvent.Evt_Close));
      }
      
      private function onSocketDataHandler(param1:ProgressEvent) : void {
         var _loc2_:* = 0;
         var _loc3_:* = 0;
         var _loc4_:* = 0;
         this._socket.readBytes(this._socketData,this._socketData.length);
         while(true)
         {
            _loc2_ = this._socketData.length;
            if(_loc2_ >= HEAD_BYTE_SIZE)
            {
               this._socketData.position = FLAG_BYTE_SIZE;
               _loc3_ = this._socketData.readInt();
               _loc4_ = this._socketData.length - _loc3_ - HEAD_BYTE_SIZE;
               if(_loc4_ >= 0)
               {
                  this.parse();
                  if(_loc4_ > 0)
                  {
                     this._cacheData.writeBytes(this._socketData,_loc3_ + HEAD_BYTE_SIZE);
                     this._socketData.position = 0;
                     this._socketData.length = 0;
                     this._socketData.writeBytes(this._cacheData);
                     this._cacheData.length = 0;
                     this._cacheData.position = 0;
                  }
                  else
                  {
                     this._socketData.length = 0;
                  }
                  this._socketData.position = 0;
                  continue;
               }
               break;
            }
            break;
         }
      }
      
      private function parse() : void {
         this._socketData.position = 0;
         var _loc1_:int = this._socketData.readByte();
         var _loc2_:int = this._socketData.readInt();
         var _loc3_:Object = new Object();
         _loc3_.data = this._socketData;
         _loc3_.flagType = _loc1_;
         _loc3_.start = HEAD_BYTE_SIZE;
         _loc3_.length = _loc2_;
         dispatchEvent(new ClientP2PEvent(ClientP2PEvent.Evt_ReceiveData,_loc3_));
      }
   }
}
